package com.kjj.gift.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.kjj.gift.clients.LoginUserClient;
import com.kjj.gift.clients.SmsValidateCodeClient;
import com.kjj.gift.domain.LoginUser;
import com.kjj.gift.domain.User;
import com.kjj.gift.dto.PhoneRegisterDto;
import com.kjj.gift.dto.RegisterPhoneValidateDto;
import com.kjj.gift.exception.asserts.Assert;
import com.kjj.gift.exception.code.StatusCode;
import com.kjj.gift.mapper.UserMapper;
import com.kjj.gift.service.IUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kjj.gift.util.AjaxResult;
import com.kjj.gift.util.StrUtils;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author kjj
 * @since 2022-10-12
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private LoginUserClient loginUserClient;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SmsValidateCodeClient smsValidateCodeClient;
    @Override
    @GlobalTransactional
    public AjaxResult registerPhone(PhoneRegisterDto phoneRegisterDto) {
        String phone = phoneRegisterDto.getPhone();
        String password = phoneRegisterDto.getPassword();
        //1.4 用户是否存在（用户名）
        List<User> users = userMapper.selectList(new EntityWrapper<User>().eq("phone", phone));
        Assert.assertListIsEmpty(users, StatusCode.CODE_400_USER_EXSIT);
        RegisterPhoneValidateDto dto = new RegisterPhoneValidateDto();
        dto.setPhone(phone);
        dto.setValidateCode(phoneRegisterDto.getSmsCode());
        //1.3 验证码校验
        AjaxResult ajaxResult = smsValidateCodeClient
                .validateRegisterCode(dto);
        if (!ajaxResult.isSuccess()) //如果没有校验通过，直接返回错误
            return ajaxResult;

        //2 保存登录信息
        User logininfo = new User();
        logininfo.setPhone(phone);
        String salt = StrUtils.getComplexRandomString(32);
        salt = "kjj" + salt + "streamxxx";
        logininfo.setSalt(salt);
        logininfo.setPassword(password);
        String uuid = "用户" + UUID.randomUUID().toString();
        logininfo.setUsername(uuid);

        logininfo.setBitState(0L);
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(uuid);
        System.out.println(uuid);
        loginUser.setTel(phone);
        loginUser.setPassword(password);
        loginUser.setType(0);    //用户是0   管理员是1
        AjaxResult ajaxResult_savelogin = loginUserClient.saveUser(loginUser);
        if (!ajaxResult_savelogin.isSuccess()){
            //抛出异常
            return ajaxResult_savelogin;
        }
        //3 保存用户信息
        Long loginUserId = Long.valueOf(ajaxResult_savelogin.getResultObj().toString());
        User user = new User();
        BeanUtils.copyProperties(logininfo,user);
        user.setId(null);
        user.setCreateTime(new Date().getTime());
        user.setLoginId(loginUserId);
        System.out.println("用户新增id:"+loginUserId);
        userMapper.insert(user);
        return AjaxResult.me();
    }
}
